const weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
String.prototype.md4 = function() {
	var v = "";
	var p = this.length;

	for (var i = 0; i < this.length; i++) {
		v += "-" + (this.charCodeAt(i) - p).toString(16);
	};
	return p.toString() + v;
};

Date.prototype.formate = function(parten) {
	if (!parten) {
		parten = "yyyy-MM-dd HH:mm:ss";
	};

	var o = {
		"M+": this.getMonth() + 1,
		/*月份 */
		"d+": this.getDate(),
		/*日 */
		"h+": this.getHours(),
		/*小时*/
		"H+": this.getHours(),
		"m+": this.getMinutes(),
		/*分 */
		"s+": this.getSeconds(),
		/*秒 */
		"q+": Math.floor((this.getMonth() + 3) / 3),
		/*季度 */
		"S": this.getMilliseconds() /*毫秒*/
	};
	if (/(y+)/.test(parten)) {

		parten = parten.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	}
	for (var k in o) {
		if (new RegExp("(" + k + ")").test(parten)) {
			parten = parten.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
		}
	}
	return parten;

}
Date.prototype.dayofweek = function() {
	return weekday[this.getDay() + 1];
}

module.exports = {
	User: null,
	JSON: {
		merge: function(targetObj, fromObj) {
			if (!targetObj || !fromObj || !targetObj instanceof Object || !targetObj instanceof Object) {
				return targetObj;
			};

			if (fromObj instanceof Array && targetObj instanceof Array) {
				for (var i = 0; i < fromObj.length; i++) {
					if (fromObj[i] instanceof Object && targetObj[i]) {
						targetObj[i] = JSON.clone(fromObj[i]);
					} else {
						targetObj[i] = fromObj[i];
					}
				}
			} else {
				for (var p in fromObj) {
					if (targetObj.hasOwnProperty(p) && targetObj[p] instanceof Object) {
						targetObj[p] = JSON.clone(fromObj[p]);
					} else {
						targetObj[p] = fromObj[p];
					}
				}
			}
			return targetObj;
		},
		clone: function(obj) {
			if (!obj || !obj instanceof Object) {
				return obj;
			}
			return JSON.parse(JSON.stringify(obj));
		}
	},
	Number:{
		fixLength:function(num,len){
			var ln = num.toString().length;
			while (ln < len) {
				num = "0" + num;
				ln += 1;
			}
			return num.toString();
		}
	}
}
